package com.tfr.core.utility;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class DataFormatter {

	/**
	 * This Method is now Obslete
	 * 
	 */
	public static String getDataFromFile(String path) {
		StringBuilder sb = new StringBuilder();
		BufferedReader br;
		try {
			br = new BufferedReader(new FileReader(path));
			String line;
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		String nohtml = DataFormatter.filterformatString(sb.toString());
		return nohtml;
	}

	public static String filterformatString(String sb) {
		String nohtml = sb.replaceAll("\\<.*?>", ",");
		nohtml = nohtml.replaceAll("\\(", "");
		nohtml = nohtml.replaceAll("\\)", "");
		nohtml = nohtml.replaceAll("\\[.*?]", "");
		nohtml = nohtml.replaceAll("\\&nbsp;", ",");
		nohtml = nohtml.replaceAll("\\&ndash;", "-");
		nohtml = nohtml.replaceAll("\\&amp;", "&");
		nohtml = nohtml.replaceAll("\\_", ",");
		nohtml = nohtml.replaceAll("\\n", ",");
		nohtml = nohtml.replaceAll("\\r|\\n", ",");
		nohtml = nohtml.replaceAll(",+", ",");
		return nohtml;
	}

	/**
	 * This Method is now Obslete
	 * 
	 */

	public static ArrayList<ArrayList<String>> matchSequentialPattern(
			String inputString, List<String> KEY_WORDS) {
		ArrayList<ArrayList<String>> listOfLabelsAndValues = new ArrayList<ArrayList<String>>();

		/**
		 * Starting to the first keyword matching
		 * 
		 */
		ArrayList<String> singleLabelAndValueFirst = new ArrayList<String>();
		Pattern patternFirst = Pattern.compile(KEY_WORDS.get(0));
		Matcher matcherFirst = patternFirst.matcher(inputString);
		while (matcherFirst.find()) {
			singleLabelAndValueFirst
					.add(StringUtils.strip(
							inputString.substring(0, matcherFirst.start())
									.toString().trim(), ","));
			listOfLabelsAndValues.add(singleLabelAndValueFirst);
		}

		/**
		 * Mid keyword matching and extracting texts
		 * 
		 */

		boolean found = false;
		for (int i = 0; i < KEY_WORDS.size(); i++) {
			ArrayList<String> singleLabelAndValue = new ArrayList<String>();
			for (int j = 0; j < KEY_WORDS.size(); j++) {
				Pattern pattern = Pattern.compile("(?<=" + KEY_WORDS.get(i)
						+ ").*.(?=" + KEY_WORDS.get(j) + ")");
				Matcher matcher = pattern.matcher(inputString);
				if (found == true) {
					break;
				}
				while (matcher.find()) {
					singleLabelAndValue.add(KEY_WORDS.get(i).toString().trim());
					singleLabelAndValue.add(StringUtils.strip(matcher.group()
							.toString().toString().trim(), ","));
					found = true;
				}
			}
			found = false;
			listOfLabelsAndValues.add(singleLabelAndValue);
		}

		/**
		 * Last keyword to the end of the string matching
		 * 
		 */
		ArrayList<String> singleLabelAndValueLast = new ArrayList<String>();
		Pattern patternLast = Pattern.compile("(?<="
				+ KEY_WORDS.get(KEY_WORDS.size() - 1) + ").*.(?=$)");
		Matcher matcherLast = patternLast.matcher(inputString);

		while (matcherLast.find()) {
			singleLabelAndValueLast.add((KEY_WORDS.get(KEY_WORDS.size() - 1))
					.toString().trim());
			singleLabelAndValueLast.add(StringUtils.strip(matcherLast.group()
					.toString().toString().trim(), ","));
			listOfLabelsAndValues.add(singleLabelAndValueLast);
		}
		return listOfLabelsAndValues;
	}
}
